<?php

namespace Biz\model\SamsungActivity;

use Doraemon\pockets\datebase\BaseDbModel;
use Doraemon\tools\HhzEnv;
use Doraemon\tools\ImgUrl;

class SamsungJoinUserListModel extends BaseDbModel
{

    public const _CONFIG_FILE_ = 'db_store';


    public const _CONFIG_SELECT_ = 'db_brand_activity';

    public const _DATABASE_ = 'brand_activity';


    public const _TABLE_NAME = 'samsung_join_user_list';

    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;
    const STATUS_SHOW = 2; //可在作品广场展示并参与投票
    const STATUS_PUBLIC = 3; //已获奖作品

    const RELATION_OBJ_ID_DEV = '00054yz000000hm5';
    const RELATION_OBJ_ID_QA = '0009vzb0000bonyd';
    const RELATION_OBJ_ID_GRAY = '005y49f00007uw3u';
    const RELATION_OBJ_ID_PRO = '0060flc000063a7b';

    const SECRET_SALT = 'tw8vEq27am64lRHg7IlnGrXkaggkyXtU'; //加密字符串加盐


    const VOTE_END_TIME_STAMP = 1698461477; //投票结束时间戳


    const ARTIST_MAP = [ //艺术家列表
        [
            'artist_id' => 1,
            'name' => '克劳德·莫奈',
            'pic' => 'https://img.haohaozhu.cn/App-imageShow/o_phbig/f2b/3321f30ie0ju0hW00rjqnrx?h=1745&iv=1&w=1618&sw=0c0',
            'background' => 'https://img.haohaozhu.cn/Op-imageShow/8640a30ku14q0Xw00rjo8w5?iv=1',
        ],
        [
            'artist_id' => 2,
            'name' => '宋徽宗·赵佶',
            'pic' => 'https://img.haohaozhu.cn/App-imageShow/o_phbig/4d0/4f0bf30ie0ju08600rjqnsq?h=1745&iv=1&w=1618&sw=0c6',
            'background' => 'https://img.haohaozhu.cn/Op-imageShow/5f06530ku14q0hB00rjo8w6?iv=1',
        ],
        [
            'artist_id' => 3,
            'name' => '达·芬奇',
            'pic' => 'https://img.haohaozhu.cn/App-imageShow/o_phbig/3bf/f8d1430ie0ju0xK00rjqno1?h=1745&iv=1&w=1618&sw=a0b',
            'background' => 'https://img.haohaozhu.cn/Op-imageShow/f497130ku14q0I300rjo8w3?iv=1',
        ],
        [
            'artist_id' => 4,
            'name' => '勒内·马格里特',
            'pic' => 'https://img.haohaozhu.cn/App-imageShow/o_phbig/0a3/7cc0730ie0ju0Vb00rjqnra?h=1745&iv=1&w=1618&sw=a14',
            'background' => 'https://img.haohaozhu.cn/Op-imageShow/16d2430ku14q0ns00rjo8w4?iv=1',
        ],
        [
            'artist_id' => 5,
            'name' => '安徒生',
            'pic' => 'https://img.haohaozhu.cn/App-imageShow/o_phbig/449/557fe30ie0ju0fi00rjqnjn?h=1745&iv=1&w=1618&sw=394',
            'background' => 'https://img.haohaozhu.cn/App-imageShow/o_phbig/937/ffcbf30ku14q03W00rjqnnh?h=3162&iv=1&w=1618&sw=3a6',
        ],
        [
            'artist_id' => 6,
            'name' => '安东尼奥·高迪',
            'pic' => 'https://img.haohaozhu.cn/App-imageShow/o_phbig/c6c/d71e030ie0ju0eF00rjqnpw?h=1745&iv=1&w=1618&sw=81',
            'background' => 'https://img.haohaozhu.cn/App-imageShow/o_phbig/66b/e8b4a30ku14q0m300rjqnqs?h=3162&iv=1&w=1618&sw=569',
        ],
    ];

    const PROFESSION_MAP = [ // 职业列表
        [
            'id' => 1,
            'name' => '设计师',
        ],
        [
            'id' => 2,
            'name' => '学生',
        ],
        [
            'id' => 3,
            'name' => '上班族',
        ],
        [
            'id' => 4,
            'name' => '自由职业者',
        ],
    ];

    const TV_TYPE_MAP = [ // 电视型号
        [
            'id' => 1,
            'name' => '画壁融入屏',
        ],
        [
            'id' => 2,
            'name' => '画境融入屏',
        ],
        [
            'id' => 3,
            'name' => 'Neo QLED 8K电视',
        ],
        [
            'id' => 4,
            'name' => 'Micro LED',
        ],
    ];


    public static function insertData($data)
    {
        if (!isset($data['addtime'])) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return self::id();
    }

    public static function updateDataByWhere($data, $where): bool
    {
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return true;
    }

    public static function selectByWhere($where, $field = '*'): array
    {
        $result = [];
        $list = self::select($field, $where);
        if ((is_array($field) || $field == '*') && !empty($list)) {
            foreach ($list as $row) {
                $row['imgs_ori_url'] = [];
                $info = self::formatInfo($row);
                $result[] = $info;
            }
        }

        return $result;
    }

    public static function formatInfo($info)
    {
        foreach ($info as $key => $val) {
            switch ($key) {
                case 'avatar':
                    $info['avatar_url'] = $val ? ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $val,
                        ImgUrl::IMAGE_SIZE_ORI_NPHONE) : '';
                    break;
                case 'profession_id':
                    $info['profession_name'] = self::PROFESSION_MAP[$val - 1]['name'] ?? '';
                    break;
                case 'tv_type_id':
                    $info['tv_type_name'] = self::TV_TYPE_MAP[$val - 1]['name'] ?? '';
                    break;
                case 'artist_id':
                    $info['artist_name'] = self::ARTIST_MAP[$val - 1]['name'] ?? '';
                    break;
                case 'cover_img':
                    $info['cover_img_url'] = $val ? ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $val,
                        ImgUrl::IMAGE_SIZE_ORI_NPHONE) : '';
                    break;
                case 'imgs':
                    if (!empty($val)) {
                        $val = json_decode($val);
                        foreach ($val as $picId) {
                            $info['imgs_url'][] = $picId ? ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $picId,
                                ImgUrl::IMAGE_SIZE_ORI_NPHONE) : '';
                            $info['imgs_ori_url'][] = $picId ? ImgUrl::getBizOriginUrl($picId) : '';
                        }
                    }
                    break;
                default;
            }

        }

        return $info;
    }

    public static function getByWhere($where, $field = '*')
    {
        $info = self::get($field, $where);
        if ((is_array($field) || $field == '*') && !empty($info)) {
            $info['imgs_ori_url'] = [];
            $info = self::formatInfo($info);
        }

        return $info;
    }

    public static function getRelationObjId(): string
    {
        if (HhzEnv::isShopTest()) {
            $objId = self::RELATION_OBJ_ID_DEV;//开发环境
        } else {
            if (HhzEnv::isTest()) {
                $objId = self::RELATION_OBJ_ID_QA;//好好测
            } else {
                if (HhzEnv::isGray()) {
                    $objId = self::RELATION_OBJ_ID_GRAY;//灰度
                } else {
                    $objId = self::RELATION_OBJ_ID_PRO;//线上
                }
            }
        }

        return $objId;
    }

    public static function isCanVote(): int
    {
        //如果投票结束时间大于当前时间 则说明活动进行中 返回1 否则表示活动结束返回0
        return (int)(self::VOTE_END_TIME_STAMP >= time());
    }
}